package org.teamh.connection.json.handler;

import org.teamh.connection.json.event.JsonEventObject;
import org.teamh.game.Start;
import org.teamh.logic.datamodel.*;

public class WaypointHandler extends JsonMasterHandler {

	public WaypointHandler(Start start, DatamodelManager manager) {
		super(start, manager);
	}
	
	public boolean handleMsg(JsonEventObject json) {
		
		if (json.getSource().startsWith("Waypoint")) {
			if (json.getProperty().equals("cell")) {
				manager.getWaypoint(json.getSource()).setCell(manager.getCell(json.getNewValue()));
				return true;
			} else if (json.getProperty().equals("next")) {
				if (json.getNewValue() != null) {
					Waypoint waypoint = new Waypoint(json.getNewValue());
					manager.addWaypoint(waypoint);
					manager.getWaypoint(json.getSource()).setNext(manager.getWaypoint(json.getNewValue()));
					start.getModel().setCount(start.getModel().getCount() + 1);
					manager.getWaypoint(json.getSource()).setIndex(start.getModel().getCount());
				}
				return true;
			} else if (json.getProperty().equals("previous")) {
				if (json.getNewValue() != null) {
					manager.getWaypoint(json.getSource()).setPrevious(manager.getWaypoint(json.getNewValue()));
				}
				return true;
			}
			return false;
		} else {
			return false;
		}	
	}
}